//
//  MyPartentViewController.m
//  HubeiHotel
//
//  Created by LiBenCan on 17/11/22.
//  Copyright © 2017年 张露杰. All rights reserved.
//

#import "MyPartentViewController.h"
#import "TextFiledView.h"
#import "SelfTextView.h"
@interface MyPartentViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView *tableV;

@property (nonatomic,strong)NSArray *titleArr;//标题数组

@property (nonatomic,strong)NSString *nameStr;//联系人
@property (nonatomic,strong)NSString *phoneStr;//电话号码
@property (nonatomic,strong)NSString *discribStr;//备注
@property (nonatomic,strong)UIButton *saveBtn;//提交按钮
@end

@implementation MyPartentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initTitleWithTitle:@"我要合作"];
    _titleArr = @[@[@"联系人",@"名字"],@[@"联系电话",@"电话号码"]];
    [self.view addSubview:self.tableV];
    [self.view addSubview:self.saveBtn];
}

-(UITableView *)tableV//创建View
{
    if (!_tableV) {
        _tableV = [[UITableView alloc] initWithFrame:CGRectMake(0,0, SCREENW, 300*screenW_6) style:UITableViewStylePlain];
        _tableV.rowHeight = 50*screenW_6;
        _tableV.backgroundColor = backColor;
        _tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableV.showsVerticalScrollIndicator = NO;
        _tableV.scrollEnabled = NO;
        _tableV.delegate = self;
        _tableV.dataSource = self;
    }
    return _tableV;
}
#pragma Mark TableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.000001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 190*screenW_6;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    }
    TextFiledView *textF = [[TextFiledView alloc]initWithFrame:CGRectMake(0,0, SCREENW, 50*screenW_6) PlaceHodel:_titleArr[indexPath.row][1] Title:_titleArr[indexPath.row][0] text:@""];
    textF.backgroundColor = [UIColor whiteColor];
    textF.tfBlock = ^(NSString *tfStr){
        if (indexPath.row == 0) {//联系人
            _nameStr = tfStr;
        }else//联系电话
        {
            _phoneStr = tfStr;
        }
    };
    [cell addSubview:textF];
    return cell;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, 190*screenW_6)];
    view.backgroundColor = [UIColor whiteColor];
    
    //标题
    UILabel *titleLab = [TomamaUI createLabel:CGRectMake(25*screenW_6,15*screenW_6,SCREENW-150*screenW_6,25*screenW_6) textAligment:0 font:[UIFont systemFontOfSize:16*screenW_6] textColor:[UIColor colorWithHexString:@"#333333"] text:@"备注说明"];
    [view addSubview:titleLab];
    
    //输入框
    SelfTextView *inputTV = [[SelfTextView alloc]initWithFrame:CGRectMake(20*screenW_6, 50*screenW_6, SCREENW-40*screenW_6, 120*screenW_6) PlaceHodel:@"请输入说明的内容"];
    inputTV.backgroundColor = backColor;
    inputTV.TextV.backgroundColor = backColor;
    inputTV.blcok = ^(NSString *text){
        _discribStr = text;
    };
    [view addSubview:inputTV];
    
    view.frame = CGRectMake(0, 0, SCREENW, CGRectGetMaxY(inputTV.frame) + 10*screenW_6);
    return view;
}

-(UIButton *)saveBtn//提交按钮
{
    if (!_saveBtn) {
        _saveBtn = [TomamaUI createButton:UIButtonTypeCustom Frame:CGRectMake(15*screenW_6,CGRectGetMaxY(self.tableV.frame)+30*screenW_6, SCREENW-30*screenW_6, 45*screenW_6) targ:self sel:@selector(saveSelectBtn) titleColor:[UIColor whiteColor] font:[UIFont systemFontOfSize:17*screenW_6] title:@"提交" image:@"" backGroundImage:@""];
        _saveBtn.backgroundColor = [UIColor colorWithHexString:@"#59cbb3"];
        [_saveBtn.layer setCornerRadius:5*screenW_6];
        _saveBtn.layer.masksToBounds = YES;
    }
    return _saveBtn;
}

-(void)saveSelectBtn//提交事件
{
    if (_nameStr.length == 0) {
        [TomamaUI creatAlertWithTarget:self message:@"请输入姓名" timeInterval:2];
        return;
    }
    if (_phoneStr.length != 11) {
        [TomamaUI creatAlertWithTarget:self message:@"请输入正确的手机号码" timeInterval:2];
        return;
    }
    if (_discribStr.length == 0) {
        [TomamaUI creatAlertWithTarget:self message:@"请输入备注说明" timeInterval:2];
        return;
    }
    [HudHelper showProgressHudWithMessage:@"" toView:self.view];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [HttpRequest PostrequestUrl:@"/other/addCooperativeRecord" Dic:@{@"contacts":_nameStr,@"contactsPhone":_phoneStr,@"remarks":_discribStr} completion:^(NSDictionary *responseDic) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [HudHelper hideHudToView:self.view];
                if ([responseDic[@"success"] boolValue]) {
                    [TomamaUI creatAlertWithTarget:self message:@"提交成功" timeInterval:2 block:^{
                        [self.navigationController popViewControllerAnimated:YES];
                    }];
                }else{
                    [TomamaUI creatAlertWithTarget:self message:responseDic[@"msg"] timeInterval:2];
                }
            });
        }];
    });
}

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event{
    
    [self.view endEditing:YES];
    
}

@end
